package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.modular.system.entity.BtyDkmx;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyDkmxMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案管理表 服务实现类
 * </p>
 *
 * @author liuy
 * @since
 */
@Service
public class BtyDkmxService extends ServiceImpl<BtyDkmxMapper, BtyDkmx> {
    /**
     * 获取打款明细列表
     */
    public Page<Map<String, Object>> list(String condition,String flag) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition,flag);
    }
    /**
     * 获取打款明细列表
     */
    public List<BtyDkmx> msg(String id,String condition,String flag) {
        /*if(id!=null){
            long l = Long.parseLong(id);
        }*/
        return baseMapper.msg(id,condition,flag);
    }
    /**
     * 导入txt
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public Boolean batchImportTxt(String fileName, MultipartFile multipartFile){
        boolean notNull = false;
        BtyDkmx btyDkmx=new BtyDkmx();
        try {
            //截取日期
            String dealTime=null;
            if(fileName.length()>8) {
                dealTime = fileName.substring(0, 8);
            }
            if(null!=multipartFile){
            String suffixName=fileName.substring(fileName.lastIndexOf("."));
            if(suffixName.equals(".txt")){
                FileInputStream fileInputStream=(FileInputStream)multipartFile.getInputStream();
                InputStreamReader isr=new InputStreamReader(fileInputStream,"utf-8");
                BufferedReader br=new BufferedReader(isr);
                String lineTxt;
                br.readLine();//忽略第一行标题
                while((lineTxt=br.readLine())!=null){
                    String[] s=lineTxt.split("\\|");
                    System.out.println("length:"+s.length);
                    System.out.println("lineTxt:"+lineTxt);
                    btyDkmx.setDealTime(dealTime);
                    btyDkmx.setFkzh(s[1]);
                    btyDkmx.setSkrzh(s[2]);
                    //btyDkmx.setPhone(s[3]);
                    btyDkmx.setName(s[3]);
                    btyDkmx.setZzje(s[4]);
                    btyDkmx.setRemark(s[5]);
                    btyDkmx.setStatus(s[6]);
                    btyDkmx.setSxf(s[7]);
                    if(s.length==9){
                        btyDkmx.setSbyy(s[8]);
                    }

                    btyDkmx.setCreateTime(new Date());
                    baseMapper.insert(btyDkmx);
                }
               notNull=true;
            }

            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return notNull;
    }


    /**
     * 修改打印状态
     */
    public int updateFlag(String id) {
        String[] split = id.split(",");
        return baseMapper.updateFlag(split);
    }

}
